package com.du.web;
/**
 * @author : DuJiabao
 * @Date : 2021/1/31 22:31
 * @Project : bookstore
 * @File : ${FILE_NAME}
 * @Desc :
 */

import com.du.pojo.User;
import com.du.service.UserService;
import com.du.service.impl.UserServiceImpl;
import com.du.utils.WebUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

@WebServlet(name = "UserServlet", value = "/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 处理login的action
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 优雅地将参数导入到新的bean中
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = WebUtils.copyParamToBean(parameterMap, new User());
        if (userService.loginUser(user) != null) {
            System.out.println("登录成功！");
            request.getRequestDispatcher("pages/user/login_success.jsp").forward(request, response);
            return;
        }
        System.out.println("登录失败");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 将一些信息,返回给界面上输出
        request.setAttribute("msg", "用户名或密码错误");
        request.setAttribute("username", username);
        request.setAttribute("password", password);
        request.getRequestDispatcher("pages/user/login.jsp").forward(request, response);
    }

    /**
     * 处理regist的action
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repwd = request.getParameter("repwd");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        request.setAttribute("username", username);
        request.setAttribute("password", password);
        request.setAttribute("repwd", repwd);
        request.setAttribute("email", email);
        request.setAttribute("code", code);

        if (!code.equalsIgnoreCase("abc")) {
            System.out.println("验证码错误");
            request.setAttribute("msg", "验证码错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            return;
        }

        if (userService.existsUser(username)) {
            System.out.println(username + " 该用户名已存在");
            request.setAttribute("msg", "该用户名已存在");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            return;
        }

        // 优雅地将参数放到bean上
        User user = WebUtils.copyParamToBean(request.getParameterMap(), new User());
        userService.registerUser(user);
        System.out.println(username + " 注册用户成功");
        request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
    }
}

